﻿using System;
using DMS.Base;
using System.Data;
using DMS.Business.BaseClass;

namespace DMS.Business.Utility
{
    public class WorkFlowAttachmentHelper : BaseService
    {
        public static void AddNewAttachment(string instanceID,  string attachmentName, string attachURL)
        {
            string sql = @"INSERT INTO WF_Attachment(AttachID,InstanceID,AttachName,AttachURL,CreatorID,CreatorName,Created) 
                            VALUES(NEWID(),'{0}','{1}','{2}','{3}','{4}','{5}')";
            sql = string.Format(sql, instanceID, attachmentName, attachURL, UserHelper.GetContextUser().ID.ToString(), 
                UserHelper.GetContextUser().Name, DateTime.Now.ToString());

            DBHelper.Instance.ExecuteNonQuery(CommandType.Text, sql);

        }

        public static DataSet GetAttachmentByInstanceID(string instanceID)
        { 
            DataSet ds = null;

            string sql = string.Format("SELECT * FROM WF_Attachment WHERE InstanceID='{0}'",instanceID);
            ds = DBHelper.Instance.ExecuteDataSet(CommandType.Text, sql);

            return ds;
        }

        public static void DeleteAttachment(string attachURL)
        {
            string sql = @"DELETE WF_Attachment where AttachURL='{0}'";
            sql = string.Format(sql, attachURL);

            DBHelper.Instance.ExecuteNonQuery(CommandType.Text, sql);
        }


        public static DataSet GetAttachmentByDistributionFormID(string DistributionFormID)
        {
            DataSet ds = null;

            string sql = string.Format(@"SELECT AT.* FROM WF_Attachment AT
                                        JOIN dbo.MA_MaterialApplyForms MAF ON MAF.InstanceID = AT.InstanceID
                                        JOIN dbo.DF_DisFormDetail DFD ON DFD.PurOrApplyFormID=MAF.MaterApplyFormID
                                        WHERE DFD.DisFormID='{0}'", DistributionFormID);
            ds = DBHelper.Instance.ExecuteDataSet(CommandType.Text, sql);

            return ds;
        }
    }
}
